package com.gzhryc.shared_device.oem.h5.agent.mng.jetty;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.WebUtils;

public class CommonFilter implements Filter {
	
	final static Logger log = Logger.getLogger(CommonFilter.class);
	
	private static Pattern _Pat = Pattern.compile("^/agent_mng(/.*)$");
	
	private static Pattern _Pat4 = Pattern.compile("^/agent_mng(/[0-9a-z]+/[apdmf]\\w+/images/.*)$");		//本地图片
	private static Pattern _Pat5 = Pattern.compile("^/agent_mng(/[0-9a-z]+/judao/icon/.*)$");		//本地图片

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String localDir = PropertyTools.get().getString("localDir","");
		if(StringTools.isNotBlank(localDir)) {
			_Pat4 = Pattern.compile("^/agent_mng("+localDir+"/\\w+/images/.*)$");
		}
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = HttpServletRequest.class.cast(request);
		HttpServletResponse httpResponse = HttpServletResponse.class.cast(response);

		String uri = httpRequest.getRequestURI();
		if(uri.contains("/favicon.ico")) {
			return;
		}
		
		try {
			// 处理普通静态资源
			if (uri.contains("/layuiadmin/")) {
				String filePath = uri;
				Integer i = uri.indexOf("/layuiadmin/");
				if (i > 0) {
					filePath = uri.substring(i);
				}
				
				if(filePath.equals("/layuiadmin/tpl/system/about.html")){
					httpResponse.sendRedirect(httpRequest.getContextPath() + "/about.html");
				}else {
					WebUtils.setResponseContentType(httpResponse, uri);
					filePath = httpRequest.getServletContext().getRealPath(URLDecoder.decode(filePath, "UTF-8"));
					WebUtils.readResource(httpResponse, filePath);
				}
				return;
			}
		} catch (Exception e) {
			log.error("GET请求处理静态资源错误", e);
		}
		
		try {
			// 处理普通静态资源
			if (uri.contains("/assets/")) {
				String filePath = uri;
				Integer i = uri.indexOf("/assets/");
				if (i > 0) {
					filePath = uri.substring(i);
				}

				String path = PropertyTools.get().getString("localResPath") + filePath;
				WebUtils.setResponseContentType(httpResponse, uri);
				WebUtils.readResource(httpResponse, path);
				return;
			}
		} catch (Exception e) {
			log.error("GET请求处理静态资源错误", e);
		}
		
		//匹配本地资源
		Matcher mat4 = _Pat4.matcher(uri);
		if (mat4.find()) {
			String path = PropertyTools.get().getString("localResPath")+ mat4.group(1);;
			WebUtils.setResponseContentType(httpResponse, uri);
			WebUtils.readResource(httpResponse, path);
			return;
		}
		
		Matcher mat5 = _Pat5.matcher(uri);
		if (mat5.find()) {
			String path = PropertyTools.get().getString("localResPath") + mat5.group(1);;
			WebUtils.setResponseContentType(httpResponse, uri);
			WebUtils.readResource(httpResponse, path);
			return;
		}

		Matcher matcher = _Pat.matcher(uri);
		if(matcher.find()) {
			httpRequest.setAttribute("uri", matcher.group(1));
		}else {
			httpRequest.setAttribute("uri", uri);
		}

		chain.doFilter(httpRequest, httpResponse);
		return;
	}

	@Override
	public void destroy() {}

}
